<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card>
      <el-row>
        <el-col :span="8">
          <el-input
              v-model="queryInfo.query"
              placeholder="请输入搜索内容"
              class="input-with-select"
              clearable
              @clear="getGoodsList"
          >
            <template #append>
              <el-button :icon="Search()" @click="getGoodsList"/>
            </template>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" style="margin-left: 20px" @click="goAddPage">
            添加商品
          </el-button>
        </el-col>
      </el-row>

      <el-table :data="goodList" border stripe>
        <el-table-column type="index"/>

        <el-table-column label="商品名称" prop="goods_name"></el-table-column>
        <el-table-column label="商品价格(元)" width="95" prop="goods_price"/>
        <el-table-column label="商品重量" width="95" prop="goods_weight"/>
        <el-table-column label="创建时间" width="130" prop="add_time">
          <!--          <template v-slot:default="s">-->
          <!--            {{s.row.add_time | dateFormat}}-->
          <!--          </template>-->
        </el-table-column>
        <el-table-column label="操作" width="130">
          <template v-slot:default="s">
            <el-button type="primary" size="small" :icon="Edit()">
            </el-button>
            <el-button type="danger" size="small" :icon="Delete()" @click="removeById(s.row.goods_id)"></el-button>
          </template>
        </el-table-column>

      </el-table>

      <el-pagination
          v-model:current-page="queryInfo.pagenum"
          v-model:page-size="queryInfo.pagesize"
          :page-sizes="[5, 10, 15, 20]"
          layout="total, sizes, prev, pager, next, jumper"
          background
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
      />


    </el-card>
  </div>
</template>

<script>

import {Delete, Edit, Search} from "@element-plus/icons-vue";

export default {
  name: "List",
  created() {
    this.getGoodsList()
  },
  computed: {},
  data() {
    return {
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      goodList: [],
      total: 0,
      currentPage: 0,

    }
  },
  methods: {
    Delete() {
      return Delete
    },
    Edit() {
      return Edit
    },
    Search() {
      return Search
    },
    async getGoodsList() {
      const {data: res} = await this.$http.get('/goods', {params: this.queryInfo})

      if (res.meta.status === 200) {
        this.goodList = res.data.goods
        this.total = res.data.total
      }
      return (res.meta.status === 200) ? null : this.$message.error(res.meta.msg);
    },
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      this.getGoodsList()

    },
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage
      this.getGoodsList()
    },
    removeById(id){
      this.$confirm(
          '此操作将永久删除用户，请确认！',
          '警告',
          {
            confirmButtonText: '确认',
            cancelButtonText: '取消',
            type: 'warning',
          }
      ).then(async () => {
        const {data: res} = await this.$http.delete(`goods/${id}`);

        if(res.meta.status !== 200){
          this.$message.warning(res.meta.msg)
        }else{
          this.$message.success(res.meta.msg)
          await this.getGoodsList()
        }

      }).catch(() => {
        this.$message.info('已取消删除操作');
      })
    },
    goAddPage(){
      this.$router.push('add_goods')
    },

  }
}
</script>

<style scoped>

</style>